package com.briup.pts.service.impl;

import com.briup.pts.domain.po.AnalysisPO;
import com.briup.pts.mapper.AnalysisMapper;
import com.briup.pts.service.AnalysisService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AnalysisServiceImpl implements AnalysisService {

    private final AnalysisMapper analysisMapper;

    @Override
    public Map<String, List<?>> count() {
        List<AnalysisPO> pos = analysisMapper.count();

        // 将pos内的数据，根据属性名和属性值的区别分别写入到两个 List集合中
        List<String> names = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        pos.forEach(po->{
            names.add(po.getName());
            values.add(po.getValue());
        });

        return Map.of("name",names,"value",values);
    }

    @Override
    public Map<String, Integer> indexCount() {
        List<AnalysisPO> analysisPOS = analysisMapper.indexCount(30, 50);
        // 处理analysisPOS，将List集合中存储的数据 转换为 Map集合存储的数据
        return analysisPOS.stream()
                .collect(Collectors
                        .toMap(AnalysisPO::getName,
                                AnalysisPO::getValue));
    }

    @Override
    public Map<String, Integer> countDisease() {
        List<AnalysisPO> analysisPOS = analysisMapper.countDisease();
        // 处理analysisPOS，将List集合中存储的数据 转换为 Map集合存储的数据
        return analysisPOS.stream()
                .collect(Collectors
                        .toMap(AnalysisPO::getName,
                                AnalysisPO::getValue));
    }

    @Override
    public Map<String, List<?>> countSales() {
        List<AnalysisPO> pos = analysisMapper.countSales();

        // 将pos内的数据，根据属性名和属性值的区别分别写入到两个 List集合中
        List<String> names = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        pos.forEach(po->{
            names.add(po.getName());
            values.add(po.getValue());
        });

        return Map.of("name",names,"value",values);
    }
}
